package com.seafood.controller;

import com.seafood.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 */
@Tag(name = "系统测试", description = "系统功能测试API")
@RestController
@RequestMapping("/test")
public class TestController {
    
    /**
     * 公开接口测试
     */
    @Operation(summary = "公开接口", description = "无需认证的公开接口")
    @GetMapping("/public")
    public Result<Map<String, Object>> publicApi() {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "这是一个公开接口");
        data.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        data.put("status", "success");
        return Result.success("公开接口调用成功", data);
    }
    
    /**
     * 需要认证的接口
     */
    @Operation(summary = "认证接口", description = "需要登录的认证接口")
    @GetMapping("/auth")
    public Result<Map<String, Object>> authApi(Authentication authentication) {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "这是一个需要认证的接口");
        data.put("user", authentication.getName());
        data.put("authorities", authentication.getAuthorities());
        data.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return Result.success("认证接口调用成功", data);
    }
    
    /**
     * 管理员接口
     */
    @Operation(summary = "管理员接口", description = "需要管理员权限的接口")
    @GetMapping("/admin")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Map<String, Object>> adminApi(Authentication authentication) {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "这是一个管理员接口");
        data.put("user", authentication.getName());
        data.put("authorities", authentication.getAuthorities());
        data.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return Result.success("管理员接口调用成功", data);
    }
    
    /**
     * 系统状态检查
     */
    @Operation(summary = "系统状态", description = "检查系统运行状态")
    @GetMapping("/health")
    public Result<Map<String, Object>> health() {
        Map<String, Object> data = new HashMap<>();
        data.put("status", "UP");
        data.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        data.put("version", "1.0.0");
        data.put("description", "高端海产品交易平台后端服务");
        return Result.success("系统运行正常", data);
    }
}